﻿#include <windows.h>
#include <iostream>
#include "simplecom.h"

int main()
{
    HRESULT hr;
    IExample* pExample = nullptr;
    
    // 初始化COM
    hr = CoInitialize(nullptr);
    if (FAILED(hr))
    {
        std::cout << "COM initialization failed!" << std::endl;
        return -1;
    }
    
    // 创建COM组件
    hr = CoCreateInstance(CLSID_ExampleComponent, 
                         nullptr, 
                         CLSCTX_INPROC_SERVER, 
                         IID_IExample, 
                         (void**)&pExample);
    
    if (SUCCEEDED(hr) && pExample)
    {
        // 测试Add方法
        int result = 0;
        hr = pExample->Add(5, 3, &result);
        if (SUCCEEDED(hr))
        {
            std::cout << "5 + 3 = " << result << std::endl;
        }
        
        // 测试GetMessage方法
        BSTR message = nullptr;
        hr = pExample->GetMessage(&message);
        if (SUCCEEDED(hr))
        {
            std::wcout << L"Message: " << message << std::endl;
            SysFreeString(message);
        }
        
        // 测试SetMessage方法
        BSTR newMessage = SysAllocString(L"New message from client!");
        hr = pExample->SetMessage(newMessage);
        SysFreeString(newMessage);
        
        if (SUCCEEDED(hr))
        {
            hr = pExample->GetMessage(&message);
            if (SUCCEEDED(hr))
            {
                std::wcout << L"New Message: " << message << std::endl;
                SysFreeString(message);
            }
        }
        
        // 释放组件
        pExample->Release();
    }
    else
    {
        std::cout << "Failed to create COM component!" << std::endl;
    }
    
    // 清理COM
    CoUninitialize();
    
    std::cout << "Test completed!" << std::endl;
    return 0;
}